package com.fcat.common.user.rpc;


import com.alibaba.fastjson.JSONArray;
import com.fcat.common.user.config.Constants;
import com.fcat.common.user.vo.AdminClient;
import com.fcat.common.user.vo.AdminClientRole;
import org.keycloak.representations.AccessTokenResponse;
import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *
 * @author：
 * @date：2021-7-6 11:47:40
 *
 */
@FeignClient(value = "keycloak", url = "${application.keycloakUrl}", fallbackFactory = KeycloakFeignFailFactory.class)
public interface KeycloakFeign {

    /**
     * 获得token
     *
     * @param realm
     * @return
     */
    @RequestMapping(value = "/realms/{realm}/protocol/openid-connect/token", method = RequestMethod.POST)
    AccessTokenResponse token(@RequestHeader(Constants.HEADER_TOKEN_KEY) String authorization, @PathVariable("realm") String realm,
                              @RequestBody MultiValueMap<String, String> params);

    /**
     * 创建用户
     *
     * @param authorization
     * @param realm
     * @param user
     * @return
     */
    @RequestMapping(value = "/admin/realms/{realm}/users", method = RequestMethod.POST)
    void createUser(@RequestHeader(Constants.HEADER_TOKEN_KEY) String authorization, @PathVariable("realm") String realm,
                    @RequestBody UserRepresentation user);

    /**
     * 根据条件查询用户列表
     *
     * @param authorization
     * @param realm
     * @param
     * @return
     */
    @RequestMapping(value = "/admin/realms/{realm}/users", method = RequestMethod.GET)
    JSONArray findUser(@RequestHeader(Constants.HEADER_TOKEN_KEY) String authorization, @PathVariable("realm") String realm,
                       @RequestParam("username") String username);

    /**
     * 重置密码
     *
     * @param authorization
     * @param realm
     * @param id
     * @param credentialRepresentation
     * @return
     */
    @RequestMapping(value = "/admin/realms/{realm}/users/{id}/reset-password", method = RequestMethod.PUT)
    void resetPassword(@RequestHeader(Constants.HEADER_TOKEN_KEY) String authorization, @PathVariable("realm") String realm,
                       @PathVariable("id") String id, @RequestBody CredentialRepresentation credentialRepresentation);


    /**
     * 获取用户信息
     *
     * @param authorization
     * @param realm
     * @param userId
     * @return
     */
    @RequestMapping(value = "/admin/realms/{realm}/users/{id}", method = RequestMethod.GET)
    UserRepresentation getUserInfo(@RequestHeader(Constants.HEADER_TOKEN_KEY) String authorization, @PathVariable("realm") String realm,
                           @PathVariable("id") String userId);

    /**
     * 推出该用户的所有token，即，该用户所有登陆的token都无效
     *
     * @param authorization
     * @param realm
     * @param userId
     */
    @RequestMapping(value = "/admin/realms/{realm}/users/{id}/logout", method = RequestMethod.POST)
    void logout(@RequestHeader(Constants.HEADER_TOKEN_KEY) String authorization, @PathVariable("realm") String realm,
                @RequestParam("id") String userId);

    /**
     * 删除session
     *
     * @param authorization
     * @param realm
     * @param sessionId
     */
    @RequestMapping(value = "/admin/realms/{realm}/sessions/{sessionId}", method = RequestMethod.DELETE)
    void deleteSession(@RequestHeader(Constants.HEADER_TOKEN_KEY) String authorization, @PathVariable("realm") String realm,
                       @RequestParam("sessionId") String sessionId);



    /*
    获取客户端id下已分配的角色列表
     */
    @GetMapping(value = "/admin/realms/{realm}/clients/{clientId}/roles")
    List<AdminClientRole> roles(@RequestHeader(Constants.HEADER_TOKEN_KEY) String authorization, @PathVariable("realm") String realm,
                           @PathVariable("clientId") String clientId);

    /**
     * 删除
     *
     * @param authorization
     * @param realm
     * @param
     * @return
     */
    @RequestMapping(value = "/admin/realms/{realm}/users/{id}", method = RequestMethod.DELETE)
    void deleteUser(@RequestHeader(Constants.HEADER_TOKEN_KEY) String authorization, @PathVariable("realm") String realm,
                @PathVariable("id") String userId);



    /*
    获取客户端id下可分配的角色列表
     */
    //admin/realms/china/users/fc674c27-8c0a-40e6-9490-46a73118537b/role-mappings/clients/f1470a7c-4f30-4cd7-8608-8829bd25ad5e/available
    @GetMapping(value = "/admin/realms/{realm}/users/{id}/role-mappings/clients/{client}/available")
    List<AdminClientRole> available(@RequestHeader(Constants.HEADER_TOKEN_KEY) String authorization, @PathVariable("realm") String realm, @PathVariable("id") String userId
            , @PathVariable("client") String clientId);

    /**
     * 获取客户端id下已分配的角色列表
     * @param authorization
     * @param realm
     * @param userId
     * @param clientId
     * @return
     */
    //admin/realms/china/users/fc674c27-8c0a-40e6-9490-46a73118537b/role-mappings/clients/20479455-3725-48fa-8c02-738dd1b9124c/composite
    @GetMapping(value = "/admin/realms/{realm}/users/{userId}/role-mappings/clients/{clientId}/composite")
    List<AdminClientRole> composite(@RequestHeader(Constants.HEADER_TOKEN_KEY) String authorization, @PathVariable("realm") String realm, @PathVariable("userId") String userId,
                                    @PathVariable("clientId") String clientId);

    /**
     * 给用户分配角色
     * @param authorization
     * @param realm
     * @param userId
     * @param clientId
     * @param clientRoles
     * @return
     */
    //admin/realms/china/users/fc674c27-8c0a-40e6-9490-46a73118537b/role-mappings/clients/20479455-3725-48fa-8c02-738dd1b9124c
    @PostMapping(value = "/admin/realms/{realm}/users/{userId}/role-mappings/clients/{clientId}")
    List<AdminClientRole> allocateRoles(@RequestHeader(Constants.HEADER_TOKEN_KEY) String authorization, @PathVariable("realm") String realm,@PathVariable("userId") String userId,
                                    @PathVariable("clientId") String clientId,@RequestBody List<AdminClientRole> clientRoles);

    /**
     * 删除用户角色
     * @param authorization
     * @param realm
     * @param userId
     * @param clientId
     * @param clientRoles
     * @return
     */
    //http://183.6.121.161:9080/auth/admin/realms/china/users/1ec3e132-1fa7-46ce-aa18-21d60dfeb2d4/role-mappings/clients/b9ecfffe-d364-41c5-9c4a-d7810e92d65a
    @DeleteMapping(value = "/admin/realms/{realm}/users/{userId}/role-mappings/clients/{clientId}")
    List<AdminClientRole> deleteRoles(@RequestHeader(Constants.HEADER_TOKEN_KEY) String authorization, @PathVariable("realm") String realm,@PathVariable("userId") String userId,
                                 @PathVariable("clientId") String clientId,@RequestBody List<AdminClientRole> clientRoles);

    /**
     * 获取所有的客户端
     * @param authorization
     * @param realm
     */
    //http://183.6.121.161:9080/auth/admin/realms/china/clients
    @RequestMapping(value = "/admin/realms/{realm}/clients", method = RequestMethod.GET)
    List<AdminClient> clients(@RequestHeader(Constants.HEADER_TOKEN_KEY) String authorization, @PathVariable("realm") String realm);
}
